package com.aplicacion.apna;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class TabTodas extends Fragment {
	public final static String TAG_TABTEMATICA = "tab2";
	public static final String PREFERENCES = "preferences";
	
	private ArrayList<Palabra> items;
	Context context = null;
	private View parentView;
	private ListView listView = null;
	private ItemListaBaseAdapter listAdapter;
	private SharedPreferences preferences = null;
	private SharedPreferences.Editor prefEditor = null;
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		MenuBueno mainActivity = (MenuBueno) activity;
		items = mainActivity.getDictionary();
		context = activity;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		context = getActivity();

		setRetainInstance(true);
		setHasOptionsMenu(true);
		
		preferences = context.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
		prefEditor = preferences.edit();
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		context = getActivity();
		
		parentView = inflater.inflate(R.layout.tab_tematica, container, false);
		
		Collections.sort(items);
		listAdapter = new ItemListaBaseAdapter(context, items,3);
		
		listView = (ListView) parentView.findViewById(R.id.listView);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				//actualizamos el valor "fecha" del objeto que hemos seleccionado y lo guardamos en preferences
				Date now = new Date();
				SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy/HH/mm/ss");			
				
				String fecha = dateFormat.format(now);
				
				PreferencesApna.saveApnaDictionary(context, items.get(position).getTematica(), items.get(position).getPalabra(), fecha);

		        
				
				//String visto = items.get(position).getTematica()+""+items.get(position).getPalabra()+"Fecha"; 
				//prefEditor.putString(visto, df.format(now));
		       // prefEditor.commit();
		        
				//items.get(position).setFecha(now);
				
				items.get(position).setFecha(now);
				
				Intent i = new Intent(context,	Item.class);
				
				String palabra = items.get(position).getPalabra();
				String tematica = items.get(position).getTematica().toLowerCase();
				
				i.putExtra("palabra", palabra);
				i.putExtra("tematica", tematica);
				
				startActivity(i);
			}
		});
		
		listView.setAdapter(listAdapter);
		listAdapter.notifyDataSetChanged();
		
		return parentView;
	
	}
}